package com.wzy.handlerthread;

import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button startButton = (Button) findViewById(R.id.stBtn);
        Button endButton = (Button) findViewById(R.id.edBtn);
        startButton.setOnClickListener(new stBtnListener());
        endButton.setOnClickListener(new edBtnListener());
    }

    Handler handler = new Handler();

    class stBtnListener implements View.OnClickListener {
        @Override
        public void onClick(View v) {
            handler.post(updateThread);
        }
    }

    class edBtnListener implements View.OnClickListener {
        @Override
        public void onClick(View v) {
            handler.removeCallbacks(updateThread);
        }
    }

    Runnable updateThread = new Runnable() {
        @Override
        public void run() {
            System.out.println("updateThread");
            Log.v("tag", "updateThread");
            handler.postDelayed(updateThread, 2000);
        }
    };


}
